// Fluxions 3D Engine
// main.cpp
// Copyright (C) 2000-2011 Jonathan Metzgar
// 
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

#include "fluxions.hpp"


Fluxions::FluxionsInstance Graphics;


class MyApp : public Viperfish::Application
{
public:
	MyApp()
		: xrot(0), yrot(0), zrot(0), xt(0), yt(0), zt(0)
	{
	}
	
	~MyApp()
	{
	}
	
	void OnInit()
	{
	}
	
	void OnUpdate(double deltaTime)
	{
		yrot += 30 * deltaTime;
		if (yrot > 360)
			yrot -= 360;
	}
	
	void OnRender()
	{
		glEnable(GL_DEPTH_TEST);
		
		glLoadIdentity();
		glTranslatef(xt, yt, zt);
		glPushMatrix();
		glTranslatef(0, -4, -30);
		glRotatef(90, 1, 0, 0);
		glutCheckerboard(Fluxions::Colors::AntiqueWhite, Fluxions::Colors::Azure, 20, 20, -10);
		glPopMatrix();

		glEnable(GL_LIGHTING);
		glEnable(GL_LIGHT0);

		glColor4fv(Fluxions::Colors::AliceBlue);
		glPushMatrix();
		glTranslatef(0, 0, -5);

		glRotatef(xrot, 1, 0, 0);
		glRotatef(yrot, 0, 1, 0);
		glRotatef(zrot, 0, 0, 1);
		glutSolidTeapot(1.0);
		glPopMatrix();

		glDisable(GL_LIGHT0);
		glDisable(GL_LIGHTING);
		glDisable(GL_DEPTH_TEST);
	}
	
	void OnRenderOverlay()
	{
		glColor4fv(Fluxions::Colors::Lime);
		
		int y = 15;
		glutBitmapPrintLeft(GLUT_BITMAP_9_BY_15, y, "-= GLUT Template =-");
		y += 15;
		glutBitmapPrintLeft(GLUT_BITMAP_9_BY_15, y, "(C) 2009-2010 Jonathan Metzgar");
		y += 30;
	}
	
	void OnKeyDown(int key)
	{
		if (key == 27)
			exit(0);
		if (key == 'w')
			zt -= 1;
		if (key == 'a')
			xt -= 1;
		if (key == 's')
			zt += 1;
		if (key == 'd')
			xt += 1;
		if (key == 'z')
			yt += 1;
		if (key == 'c')
			yt -= 1;
		if (key == 'q')
			zrot -= 15;
		if (key == 'e')
			zrot += 15;
	}

	void OnSpecialKeyDown(int key)
	{
		if (key == GLUT_KEY_LEFT)
			yrot -= 15;
		if (key == GLUT_KEY_RIGHT)
			yrot += 15;
		if (key == GLUT_KEY_UP)
			xrot += 15;
		if (key == GLUT_KEY_DOWN)
			xrot -= 15;
	}

	void OnMouseMotion(int x, int y, int xrel, int yrel, int buttons)
	{
	}
private:
	float xrot, yrot, zrot;
	float xt, yt, zt;
	float mx, my;
};


int main(int argc, char **argv)
{
	MyApp app;

	// Set up an OpenGL context and a window
	GLUT.Init(argc, argv);
	GLUT.SetVideoMode(720, 480, 32, false);

	// Register an application so that callbacks are used as necessary
	GLUT.RegisterApplication(&app);

	// Initialize the Fluxions graphics engine (i.e. load GL extensions)
	Graphics.Init();

	// Start the main loop.  This will not return for GLUT.
	GLUT.Start();
	return 0;
}
